package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.DeskMapper;
import com.example.online_order.mapper.ShoppingcarMapper;
import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.Desk;
import com.example.online_order.pojo.Shoppingcar;
import com.example.online_order.service.ShoppingcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingcarService {
    @Autowired
    ShoppingcarMapper mapper;
    @Override
    public List<Shoppingcar> getAllProducts() {
        return mapper.getAllProducts();
    }

    @Override
    public Shoppingcar getProductById(Integer id) {
        return mapper.getProductById(id);
    }

    @Transactional
    @Override
    public Integer addToCar(Shoppingcar product) {
        Shoppingcar productDb = mapper.getProductById(product.getId());
        if (productDb != null){
            throw new BussinessException(ExceptionType.bus_id_exists,"产品已存在");
        }else {
            Integer res = mapper.addToCar(product);
            if (res != 1){
                throw new BussinessException(ExceptionType.bus_insert_fail,"添加产品失败");
            }else {
                return res;
            }
        }

    }
    @Transactional
    @Override
    public Integer deleteProduct(Integer id) {
        Shoppingcar productDb = mapper.getProductById(id);
        if (productDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"产品不存在");
        }else {
            Integer res = mapper.deleteProduct(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_delete_fail,"删除产品失败");
            }else {
                return res;
            }
        }

    }
    @Transactional
    @Override
    public Integer updatePrice(Integer id, String price) {
        Shoppingcar productDb = mapper.getProductById(id);
        if (productDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"产品不存在");
        }else {
            Integer res = mapper.updatePrice(id,price);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_update_fail,"修改价格失败");
            }else {
                return res;
            }
        }

    }
    @Transactional
    @Override
    public Integer updateQuantity(Integer id, String num) {
        Shoppingcar productDb = mapper.getProductById(id);
        if (productDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"产品不存在");
        }else {
            Integer res = mapper.updatePrice(id,num);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_update_fail,"修改价格失败");
            }else {
                return res;
            }
        }
    }
}
